package org.syg.generator;

import org.syg.generator.config.DataSourceConfig;
import org.syg.generator.entity.FreemarkerModel;
import org.syg.generator.enums.DbType;
import org.syg.generator.util.EntityInfoUtil;
import org.syg.generator.util.Generator;

import java.util.List;
import java.util.TreeSet;

/**
 * @author ps
 */
public class CodeGenerator {

    /**
     * 包路径
     */
    private String packageDir;

    /**
     * 作者
     */
    private String author;

    /**
     * 数据库配置对象
     */
    private DataSourceConfig dbConfig;

    /**
     * 数据库表集合
     */
    public TreeSet<String> tableNameList;

    public CodeGenerator(String packageDir, String author, DataSourceConfig dbConfig, TreeSet<String> tableNameList) {
        this.packageDir = packageDir;
        this.author = author;
        this.dbConfig = dbConfig;
        this.tableNameList = tableNameList;
    }

    /**
     * 生成代码
     */
    public void execute() throws Exception {
        List<FreemarkerModel> freemarkerModels = EntityInfoUtil.getPropertyInfo(author, packageDir, dbConfig, tableNameList);
        for (FreemarkerModel model : freemarkerModels) {
            Generator.createEntity(model);
        }
    }

    public static void main(String[] args) throws Exception {
        DataSourceConfig dbConfig = new DataSourceConfig();
        dbConfig.setDbType(DbType.MYSQL);
        dbConfig.setJdbcUrl("jdbc:mysql://10.94.80.40:3306/ccs_dev?useUnicode=true&useSSL=false&characterEncoding=utf8");
        dbConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dbConfig.setUsername("zycfc");
        dbConfig.setPassword("Zycfc@666");

        /** 指定表名,tableNameList可以多个 **/
        TreeSet<String> tableNameList = new TreeSet<>();
        tableNameList.add("t_ccs_apprv_call_record");

        /** 存放目录路径 */
        String packageDir = "com.ps.core";
        // 开发人姓名
        String author = "ps";
        CodeGenerator codeGen = new CodeGenerator(packageDir,author,dbConfig,tableNameList);
        codeGen.execute();
    }

}
